//
//  Finish_RootViewController.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "Finish_RootViewController.h"
#import "FinishFruitTableViewCell.h"
#import "FruitDetailViewController.h"

@interface Finish_RootViewController ()

@property (nonatomic, strong) NSMutableArray *fruits;

@end

@implementation Finish_RootViewController

- (NSMutableArray *)fruits
{
    if (!_fruits) {
        _fruits = [NSMutableArray array];
    }
    return _fruits;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fruits = [NSMutableArray arrayWithArray:[LocalData getAllFinishFruits]];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"完成";
    
    [self.view addSubview:self.tableView];
    
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = 120;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FinishFruitTableViewCell" bundle:nil] forCellReuseIdentifier:@"FinishFruitTableViewCell"];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    self.tableView.frame = CGRectMake(0, navigationBar.frame.origin.y, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - navigationBar.frame.origin.y - SL_VC_TabBar_HEIGHT);
}

# pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fruits.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FinishFruitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishFruitTableViewCell" forIndexPath:indexPath];
    cell.model = self.fruits[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FruitModel *model = self.fruits[indexPath.row];
    FruitDetailViewController *vc = [[FruitDetailViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
